﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using RibbonEditor.Rules;
using System.Windows.Forms;
using RibbonEditor.Forms.Rules;
using RibbonEditor.AppCode;

namespace RibbonEditor
{
    public class DisplayRule : IRule, IXmlBuildable, IChildDeletable
    {
        bool isParentNode = false;

        public bool IsParentNode
        {
            get { return isParentNode; }
            set { isParentNode = value; }
        }

        public string Id { get; set; }
        public List<IDisplayRule> Rules { get; set; }


        public DisplayRule(bool canHaveChildren)
        {
            try
            {
                this.Rules = new List<IDisplayRule>();
                this.isParentNode = canHaveChildren;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public DisplayRule(XmlNode node, bool canHaveChildren)
            : this(canHaveChildren)
        {
            try
            {
                this.Id = (string)XmlHelper.ExtractXmlAttributeToProperty2(node, "Id", XmlHelper.PropertyType.stringType);

                foreach (XmlNode ruleNode in node.ChildNodes)
                {
                    IDisplayRule dRule = RuleHelper.GetIDisplayRuleFromXml(ruleNode);
                    this.Rules.Add(dRule);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public string GetId()
        {
            return this.Id;
        }

        public void SetId(string id)
        {
            this.Id = id;
        }

        public void AddNode(XmlNode displayRulesNode)
        {
            try
            {
                XmlNode displayRuleNode = XmlHelper.AddNode(displayRulesNode, "DisplayRule");
                XmlHelper.AddAttribute(displayRuleNode, "Id", this.Id);

                foreach (IDisplayRule rule in this.Rules)
                {
                    rule.AddNode(displayRuleNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("DisplayRule");
                node.Tag = this;

                //if (this.isParentNode)
                //{
                node.ContextMenuStrip = this.BuildContextMenu(node);
                //}

                foreach (IDisplayRule rule in this.Rules)
                {
                    rule.BuildTreeViewElement(node);
                }

                node.Expand();

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private ContextMenuStrip BuildContextMenu(TreeNode enableRuleNode)
        {
            ContextMenuStrip cms = new ContextMenuStrip();
            cms.Tag = enableRuleNode;

            if (this.isParentNode)
            {
                ToolStripMenuItem tsmi = new ToolStripMenuItem("Add Rule");
                tsmi.Click += new EventHandler(tsmi_Click);
                cms.Items.Add(tsmi);

                ContextMenuStripHelper.AddDisplayXmlMenu(cms, true);
            }
            else
            {
                ContextMenuStripHelper.AddDisplayXmlMenu(cms, false);
            }

            ContextMenuStripHelper.AddDeleteMenu(cms, true);

            //ToolStripSeparator tsms = new ToolStripSeparator();
            //ToolStripMenuItem tsmd = new ToolStripMenuItem("Delete");
            //cms.Items.Add(tsms);
            //cms.Items.Add(tsmd);

            return cms;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            try
            {
                TreeNode selectedNode = (TreeNode)((ToolStripItem)sender).GetCurrentParent().Tag;

                RuleSelectionDialog rsDialog = new RuleSelectionDialog(RuleSelectionDialog.RuleType.DisplayRule);
                rsDialog.StartPosition = FormStartPosition.CenterParent;

                if (rsDialog.ShowDialog() == DialogResult.OK)
                {
                    IDisplayRule selectedRule = (IDisplayRule)rsDialog.SelectedRule;

                    this.Rules.Add(selectedRule);
                    selectedRule.BuildTreeViewElement(selectedNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void cms_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            //TreeNode selectedNode = (TreeNode)e.ClickedItem.GetCurrentParent().Tag;

            //switch (e.ClickedItem.Text)
            //{
            //    case "Delete":
            //        {
            //            selectedNode.TreeView.SelectedNode = selectedNode.Parent;
            //            UIActionHelper.Delete(selectedNode);
            //            //selectedNode.Parent.Nodes.Remove(selectedNode);
            //        }
            //        break;
            //    case "Add Rule":
            //        {
                        
            //        }
            //        break;
            //}
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is IDisplayRule)
                {
                    this.Rules.Remove((IDisplayRule)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
